package com.example.demo.data;

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

/**
 * @author liuhe
 * @date 2020/09/03
 */
public class StackTest {
    public static void main(String[] args) {

        testDequeLinkedList();
        testDataQueueStack();
    }

    private static void testStack() {
        Stack<String> stack = new Stack<>();
        stack.push("aaa");
        stack.push("bbb");
        stack.push("ccc");
        stack.push("ddd");
        System.out.println("最后一个元素：" + stack.peek());
        System.out.println("最后放置的一个元素：" + stack.lastElement());
        System.out.println("最先放置的一个元素：" + stack.firstElement());

        System.out.println("弹出一个元素[LIFO]:" + stack.pop());

        System.out.println(stack);
    }

    private static void testDequeLinkedList() {

        Deque<String> deques = new LinkedList<>();
        deques.push("a");
        deques.push("b");
        deques.push("c");
        deques.push("g");

        System.out.println("栈：" + deques);
        System.out.println("数据出栈[LIFO]：");
        while (!deques.isEmpty()) {
            String pop = deques.pop();
            System.out.print(pop + " ");
        }
    }

    private static void testDataQueueStack() {

        Deque<String> deques = new LinkedList<>();
        deques.offer("a");
        deques.offer("b");
        deques.offer("c");
        deques.offer("g");
        System.out.println("\n队列：" + deques);
        System.out.println("出队列[FIFO]:");
        while (!deques.isEmpty()) {
            System.out.print(deques.poll() + " ");
        }


    }
}
